//
//  LXInsuranceApplyViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/20.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXInsuranceApplyViewController.h"
#import "LXTableViewCell.h"
#import "LXChooseCarViewViewController.h"
#import "LXApplyAnnexTableCell.h"
#import "LXRemarkTabelViewCell.h"
#import "LXInsuranceModel.h"
#import "LXCooperationListViewController.h"
#import "LXCooperationModel.h"

@interface LXInsuranceApplyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSMutableDictionary *loanDic;
@property (nonatomic) NSDictionary *selDic;
@property (nonatomic) NSArray *sectionData; // 组头信息
@property (nonatomic) NSInteger picIndex; //  1交强险图片 2商业险图片


@property (nonatomic) NSMutableArray *annexAry1; //  交强险图片  展示用
@property (nonatomic) NSMutableArray *annexDataAry1; //  交强险图片 提交用

@property (nonatomic) NSMutableArray *annexAry2; //  商业险图片  展示用
@property (nonatomic) NSMutableArray *annexDataAry2; //  商业险图片 提交用

/**商业保险公司id*/
@property (nonatomic) NSString *compulsoryCompanyId;
/**交强险保险公司id*/
@property (nonatomic) NSString *businessCompanyId;

@property (nonatomic) CheLiangModel *carModel; // 车辆数据

@property (nonatomic) LXCooperationModel *cooModel; //  合作模型


@end

@implementation LXInsuranceApplyViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

   
     
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.type==1) {
        [self layoutCustomNav:self.view title:@"新增保单"];
    }
    else {
        [self layoutCustomNav:self.view title:@"修改保单记录"];
        
    }
    
    
    [self.tableView registerClass:[LXTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXTableViewCell class])];
    [self.tableView registerClass:[LXApplyAnnexTableCell class]
         forCellReuseIdentifier:NSStringFromClass([LXApplyAnnexTableCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXRemarkTabelViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
    
    NSDictionary *dic = @{
        @"url":@"upLoad_B",
    };
    self.annexAry1 = [NSMutableArray array];
    self.annexDataAry1 = [NSMutableArray array];
    [self.annexAry1 addObject:dic];
    
    self.annexAry2 = [NSMutableArray array];
    self.annexDataAry2 = [NSMutableArray array];
    [self.annexAry2 addObject:dic];
    
    
    
    [self.view addSubview:self.tableView];
    
    [self loadData];
    
    if (self.type==1) {
        [self setBottomViewWithText:@"确认新增"];
    }
    else {
        [self setBottomViewWithText:@"确认修改"];
    }
    
    
    
}
- (void)loadData{
    
    self.loanDic  = [NSMutableDictionary dictionary];
    
    
    
    // 模型为空,说明从车辆详情进入
    if (!ObjectIsEmpty(self.insuranceModel)) {
        
        // 车牌号
        [self.loanDic setValue:self.insuranceModel.carNum forKey:@"carNum"];
        
        // 给车辆模型赋值,提交时传入
        self.carModel = [CheLiangModel new];
        self.carModel.ID = self.insuranceModel.carId;
        self.carModel.carNum = self.insuranceModel.carNum;
        
        
        
        // 修改的时候需要带入数据
        if (self.type==2) {
            // 交强险保费
            [self.loanDic setValue:self.insuranceModel.compulsoryPrice forKey:@"compulsoryPrice"];
            // 交强险应付日期
            [self.loanDic setValue:self.insuranceModel.compulsoryPayDate forKey:@"compulsoryPayDate"];
            // 交强险开始时间
            [self.loanDic setValue:self.insuranceModel.compulsoryBeginTime forKey:@"compulsoryBeginTime"];
            // 交强险结束时间
            [self.loanDic setValue:self.insuranceModel.compulsoryEndTime forKey:@"compulsoryEndTime"];
            // 交强险保险公司
            [self.loanDic setValue:self.insuranceModel.compulsoryCompanyName forKey:@"compulsoryCompanyName"];
            self.compulsoryCompanyId = self.insuranceModel.compulsoryCompanyId;
            

            // 交强险保费
            [self.loanDic setValue:self.insuranceModel.businessPrice forKey:@"businessPrice"];
            // 交强险应付日期
            [self.loanDic setValue:self.insuranceModel.businessPayDate forKey:@"businessPayDate"];
            // 商业险开始时间
            [self.loanDic setValue:self.insuranceModel.businessBeginTime forKey:@"businessBeginTime"];
            // 商业险结束时间
            [self.loanDic setValue:self.insuranceModel.businessEndTime forKey:@"businessEndTime"];
            // 商业险保险公司
            [self.loanDic setValue:self.insuranceModel.businessCompanyName forKey:@"businessCompanyName"];
            self.businessCompanyId = self.insuranceModel.businessCompanyId;
            //备注
            [self.loanDic setValue:self.insuranceModel.remark forKey:@"remark"];
            //交强险图片
            for (NSString *url in self.insuranceModel.compulsoryPics) {
                NSString *imageUrl = [NSString stringWithFormat:@"%@%@",HTTP_URL,url];
                [self.annexAry1 insertObject:@{@"url":imageUrl} atIndex:0];
                [self.annexDataAry1 insertObject:url atIndex:0];
            }
            //商业险图片
            for (NSString *url in self.insuranceModel.businessPics) {
                NSString *imageUrl = [NSString stringWithFormat:@"%@%@",HTTP_URL,url];
                [self.annexAry2 insertObject:@{@"url":imageUrl} atIndex:0];
                [self.annexDataAry2 insertObject:url atIndex:0];
            }
        }
        
        
        
    }
    
    
    self.sectionData = @[
           @{
               @"title":@"",
               @"key":@"",
               @"height":@(0.00001)
           },
           @{
               @"title":@"",
               @"key":@"",
               @"height":@(10)
           },

    ];
    
    
    
    NSArray *ary0 = @[
           @{
               @"title":@"车牌号",
               @"key":@"carNum",
               @"placeHolder" : @"请选择车牌号",
               @"canEdit":@(NO),
               @"showAccess":ObjectIsEmpty(self.insuranceModel)?@(YES):@(NO),
               @"maxLength":@(1000),
               @"sel":ObjectIsEmpty(self.insuranceModel)?@"clickCarNum":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":@"交强险保单图片(最多9张)",
               @"key":@"compulsoryPics",
               @"height":@(kComAnnexHeight)
           },
           @{
               @"title":@"商业险保单图片(最多9张)",
               @"key":@"businessPics",
               @"height":@(kComAnnexHeight)
           },
           
    ];
    [self.data addObject:ary0];
    
    
    NSArray *ary1 = @[
        @{
            @"title":@"交强险信息",
            @"key":@"JQTITLE",
            @"placeHolder" : @"",
            @"textColor":ColorWith3C6CFC,
            @"canEdit":@(NO),
            @"showAccess":@(NO),
            @"maxLength":@(0),
            @"sel":@"",
            @"height":@(kComHeight)
        },
        
        @{
            @"title":self.type==1?@"*交强险保费(元)":@"交强险保费(元)",
            @"key":@"compulsoryPrice",
            @"placeHolder" : @"请输入交强险保费",
            @"canEdit":self.type==1?@(YES):@(NO),
            @"showAccess":@(NO),
            @"inputType":@(NUM_DECIMAL),
            @"keyboardType":@(UIKeyboardTypeDecimalPad),
            @"maxLength":@(8),
            @"sel":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":self.type==1?@"*应付日期":@"应付日期",
            @"key":@"compulsoryPayDate",
            @"placeHolder" : @"请选择应付日期",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":self.type==1?@"clickCompulsoryPayDate":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":self.type==1?@"*开始日期":@"开始日期",
            @"key":@"compulsoryBeginTime",
            @"placeHolder" : @"请选择交强险开始日期",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":self.type==1?@"clickJQBeginTime":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":self.type==1?@"*结束日期":@"结束日期",
            @"key":@"compulsoryEndTime",
            @"placeHolder" : @"请选择交强险结束日期",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":self.type==1?@"clickJQEndTime":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"保险公司",
            @"key":@"compulsoryCompanyName",
            @"placeHolder" : @"请输入选择公司",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(20),
            @"sel":self.type==1?@"clickCoopAction":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"商业险信息",
            @"key":@"SYTITLE",
            @"placeHolder" : @"",
            @"textColor":ColorWith3C6CFC,
            @"canEdit":@(NO),
            @"showAccess":@(NO),
            @"maxLength":@(0),
            @"sel":@"",
            @"height":@(kComHeight)
        },
        
        @{
            @"title":self.type==1?@"*商业险保费(元)":@"商业险保费(元)",
            @"key":@"businessPrice",
            @"placeHolder" : @"请输入商业险保费",
            @"canEdit":self.type==1?@(YES):@(NO),
            @"showAccess":@(NO),
            @"inputType":@(NUM_DECIMAL),
            @"keyboardType":@(UIKeyboardTypeDecimalPad),
            @"maxLength":@(8),
            @"sel":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":self.type==1?@"*应付日期":@"应付日期",
            @"key":@"businessPayDate",
            @"placeHolder" : @"请选择应付日期",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":self.type==1?@"clickBusinessPayDate":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":self.type==1?@"*开始日期":@"开始日期",
            @"key":@"businessBeginTime",
            @"placeHolder" : @"请选择保单开始日期",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":self.type==1?@"clickSYBeginTime":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":self.type==1?@"*结束日期":@"结束日期",
            @"key":@"businessEndTime",
            @"placeHolder" : @"请选择保单结束日期",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":self.type==1?@"clickSYEndTime":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"保险公司",
            @"key":@"businessCompanyName",
            @"placeHolder" : @"请输入保险公司全称",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(20),
            @"sel":self.type==1?@"clickCoopAction":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"备注",
            @"key":@"remark",
            @"placeHolder" : @"请输入备注...",
            @"canEdit":@(YES),
            @"height":@(150)
        },
        
    ];
    [self.data addObject:ary1];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.data[indexPath.section][indexPath.row];
    
    if ([dic[@"key"] isEqualToString:@"compulsoryPics"]) {
        return [LXApplyAnnexTableCell getCellHeightWithDataSource:self.annexAry1] + 30;
    }
    if ([dic[@"key"] isEqualToString:@"businessPics"]) {
        return [LXApplyAnnexTableCell getCellHeightWithDataSource:self.annexAry2] + 30;
    }
    return [dic[@"height"] doubleValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dic = self.data[indexPath.section][indexPath.row];

    // 保单
    if ([dic[@"key"] isEqualToString:@"compulsoryPics"] ||
        [dic[@"key"] isEqualToString:@"businessPics"]) {
       
        LXApplyAnnexTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXApplyAnnexTableCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
        cell.titleLab.text = dic[@"title"];
        
        if ([dic[@"key"] isEqualToString:@"compulsoryPics"]) {
            [cell reloadAnnexData:self.annexAry1 WithMaxNum:9];
        }
        else {
            [cell reloadAnnexData:self.annexAry2 WithMaxNum:9];
        }
        

        // 添加图片
        [cell setCallBackTakeAnnex:^(NSInteger num) {
            
            if ([dic[@"key"] isEqualToString:@"compulsoryPics"]) {
                self.picIndex = 1;
            }
            else {
                self.picIndex = 2;
            }
            [[LXPhotoHelper shareHelper] showImageViewSelcteWithResultBlock:^(id data) {
                
                [self uploadImage:data];
                
            } andMultipleImageBlock:^(id data) {
                
                [self uploadImage:data];
                
            } andSelectedNum:9 -  (self.picIndex==1?self.annexDataAry1.count:self.annexDataAry2.count)];
            
        }];
        
        
        // 放大图片
        [cell setCallBackExtendAnnex:^(NSInteger num,id obj) {
            
            NSArray *ary;
            if ([dic[@"key"] isEqualToString:@"compulsoryPics"]) {
                ary = self.annexDataAry1;
            }
            else {
                ary = self.annexDataAry2;
            }
            
            
            LXBigImageView *view = [[LXBigImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) WithShowImageAry:ary];
            [view.scrollView setContentOffset:CGPointMake(ScreenWidth*num, 0) animated:NO];
            [[UIApplication sharedApplication].delegate.window addSubview:view];
            
        }];
        // 删除图片
        [cell setCallBackDeleteAnnex:^(NSInteger num) {
            
            if ([dic[@"key"] isEqualToString:@"compulsoryPics"]) {
                [self.annexAry1 removeObjectAtIndex:num];
                [self.annexDataAry1 removeObjectAtIndex:num];
            }
            else {
                [self.annexAry2 removeObjectAtIndex:num];
                [self.annexDataAry2 removeObjectAtIndex:num];
            }
            

            [self.tableView reloadData];
        }];
        
        
        
        return cell;
        
        
        
    }
    else if([dic[@"key"] isEqualToString:@"remark"]){
        LXRemarkTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
        cell.textView.placeholder = dic[@"placeHolder"];
        cell.textView.maxLength = 200;
        cell.remarkTitle.text = dic[@"title"];
        cell.textView.text = self.loanDic[dic[@"key"]];
        cell.textView.userInteractionEnabled = [dic[@"canEdit"] boolValue];
        
        [cell setTextViewEdit:^(NSString * _Nonnull textStr) {
            self.loanDic[dic[@"key"]]  = textStr;
        }];
        return cell;
    }
    
    
    LXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setClipsToBounds:YES];
    

    cell.cellDic = dic;
    cell.textFiled.hidden = NO;
    cell.subTitleLab.hidden = YES;
    cell.textFiled.text = self.loanDic[dic[@"key"]];
    
    
    cell.textfiledChangeBlock = ^(NSString * _Nonnull textStr) {
        self.loanDic[dic[@"key"]]  = textStr;
    };
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic = self.data[indexPath.section][indexPath.row];
    self.selDic = dic;
    NSString * selName = dic[@"sel"];
    SEL action = NSSelectorFromString(selName);
    if ([self respondsToSelector:action]) {
        [self performSelector:action withObject:nil afterDelay:0];
    }

}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    NSDictionary *dic = self.sectionData[section];
    UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth/2, [dic[@"height"] doubleValue])];
    vv.backgroundColor = ColorWithF7F8FA;
    return vv;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    NSDictionary *dic = self.sectionData[section];
    return [dic[@"height"] doubleValue];
    
}
#pragma mark -- 选择交强险开始时间
-(void)clickJQBeginTime{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"compulsoryBeginTime"] withIsMaxDate:YES withNoForward:YES WithTips:@"请选择交强险开始日期"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        
        if([NSString compareDate:yearString withDate:self.loanDic[@"compulsoryBeginTime"] withFormat:@"yyyy-MM-dd"]==-1){
            showAlert(@"开始时间不能大于结束时间");
        }
        else {
            [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
            [weakSelf.tableView reloadData];
            
        }
    };
}
#pragma mark -- 选择交强险结束时间
-(void)clickJQEndTime{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"compulsoryEndTime"] withIsMaxDate:YES withNoForward:YES WithTips:@"请选择交强险结束日期"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        if([NSString compareDate:self.loanDic[@"compulsoryBeginTime"] withDate:yearString withFormat:@"yyyy-MM-dd"]==-1){
            showAlert(@"结束时间不能小于开始时间");
        }
        else {
            [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
            [weakSelf.tableView reloadData];
            
        }
        
    };
}
#pragma mark -- 选择交强险应付日期
-(void)clickCompulsoryPayDate{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"compulsoryPayDate"] withIsMaxDate:YES withNoForward:YES WithTips:@"请选择交强险应付日期"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
            [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
            [weakSelf.tableView reloadData];
    };
}
#pragma mark -- 选择商业险开始时间
-(void)clickSYBeginTime{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"businessBeginTime"] withIsMaxDate:YES withNoForward:YES WithTips:@"请选择商业险开始时间"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        if([NSString compareDate:yearString withDate:self.loanDic[@"businessBeginTime"] withFormat:@"yyyy-MM-dd"]==-1){
            showAlert(@"开始时间不能大于结束时间");
        }
        else {
            [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
            [weakSelf.tableView reloadData];
        }
    };
}

#pragma mark -- 选择商业险结束时间
-(void)clickSYEndTime{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"businessEndTime"] withIsMaxDate:YES withNoForward:YES WithTips:@"请选择商业险结束时间"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        if([NSString compareDate:self.loanDic[@"businessBeginTime"] withDate:yearString withFormat:@"yyyy-MM-dd"]==-1){
            showAlert(@"结束时间不能小于开始时间");
        }
        else {
            [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
            [weakSelf.tableView reloadData];
        }
       
    };
}
#pragma mark -- 选择交强险应付日期
-(void)clickBusinessPayDate{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"businessPayDate"] withIsMaxDate:YES withNoForward:YES WithTips:@"请选择商业险应付日期"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
            [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
            [weakSelf.tableView reloadData];
    };
}
#pragma mark -- 选择车牌号
- (void)clickCarNum{
    
    LXChooseCarViewViewController *vc = [[LXChooseCarViewViewController alloc] init];
    vc.comfrom = 1;
    vc.type = @"12";
    [vc setToSendValue:^(CheLiangModel * _Nonnull model) {

        self.carModel = model;
        [self.loanDic setValue:model.carNum forKey:self.selDic[@"key"]];
        [self.tableView reloadData];
        
    }];
    [self pushToNextController:vc];
}
// MARK: 选择合作公司
-(void)clickCoopAction{
    LXCooperationListViewController *vc = [[LXCooperationListViewController alloc] init];
    vc.type = 2;
    [vc setClickVcCallBlock:^(LXCooperationModel * _Nonnull model) {
        
        self.cooModel = model;
        // 交强险
        if ([self.selDic[@"key"] isEqualToString:@"compulsoryCompanyName"]) {
            [self.loanDic setValue:model.name forKey:@"compulsoryCompanyName"];
            self.compulsoryCompanyId = model.ID;
        }
        // 商业险
        if ([self.selDic[@"key"] isEqualToString:@"businessCompanyName"]) {
            [self.loanDic setValue:model.name forKey:@"businessCompanyName"];
            self.businessCompanyId = model.ID;
        }
        [self.tableView reloadData];
    }];
    [self pushToNextController:vc];
}

- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, kScreenHeight - kViewTopHeight - kComHeight - 5) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = ColorWithF7F8FA;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 0.001;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


#pragma mark ---- 确认新增/修改点击事件
- (void)clickBottomView:(UIButton *)sender{
    

    
    if (StringIsEmpty(self.loanDic[@"carNum"])) {
        showAlert(@"请选择车牌号");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"compulsoryPrice"])) {
        showAlert(@"请输入交强险保费");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"compulsoryPayDate"])) {
        showAlert(@"请选择交强险应付日期");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"compulsoryBeginTime"])) {
        showAlert(@"请选择交强险开始日期");
        return;
    }
    
    if (StringIsEmpty(self.loanDic[@"compulsoryEndTime"])) {
        showAlert(@"请选择交强险结束时间");
        return;
    }
    
    
    if (StringIsEmpty(self.loanDic[@"businessPrice"])) {
        showAlert(@"请输入商业险保费");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"businessPayDate"])) {
        showAlert(@"请选择商业险险应付日期");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"businessBeginTime"])) {
        showAlert(@"请选择商业险开始时间");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"businessEndTime"])) {
        showAlert(@"请选择商业险结束时间");
        return;
    }
    
    NSMutableDictionary *dic = @{
        
        @"carId":self.carModel.ID, // 所属车辆id
        @"carNum":self.carModel.carNum,// 所属车辆车牌号
        
        @"compulsoryPrice":self.loanDic[@"compulsoryPrice"],// 交强险保费
        @"compulsoryPayDate":self.loanDic[@"compulsoryPayDate"],// 交强险应付日期
        @"compulsoryBeginTime":self.loanDic[@"compulsoryBeginTime"], //  交强险开始时间
        @"compulsoryEndTime":self.loanDic[@"compulsoryEndTime"], //  交强险结束时间
        @"compulsoryCompanyName":self.loanDic[@"compulsoryCompanyName"], //  交强险公司名称
        @"compulsoryCompanyId":self.compulsoryCompanyId,
        
        @"businessPrice":self.loanDic[@"businessPrice"],// 商业险保费
        @"businessPayDate":self.loanDic[@"businessPayDate"],// 商业险应付日期
        @"businessBeginTime":self.loanDic[@"businessBeginTime"],//  商业险开始时间
        @"businessEndTime":self.loanDic[@"businessEndTime"], //  商业险结束时间
        @"businessCompanyName":self.loanDic[@"businessCompanyName"],// 商业险公司名称
        @"businessCompanyId":self.businessCompanyId,
       
        @"compulsoryPics":self.annexDataAry1, //  交强险保单
        @"businessPics":self.annexDataAry2,  //  商业险险保单
        @"remark":self.loanDic[@"remark"],
    
        
    }.mutableCopy;
    
    NSString *url;
    // 新增保险
    if (self.type==1) {
        url = [NSString stringWithFormat:@"%@",TinsuranceAdd_URL];
    }
    // 修改保险的时候需要穿id
    else if (self.type==2) {
        url = [NSString stringWithFormat:@"%@",TinsuranceUpdate_URL];
        [dic setValue:self.insuranceModel.ID forKey:@"id"];
    }

    
    [Tool showAlertTitle:@"是否确认提交" msg:nil cancelTitle:@"取消" commitBtnTitle:@"确定" cancelHandlerBlock:^{
    } commitHandlerBlock:^{
        [MSNetworkManager postRevertUrl:url params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            if (isSuccess) {
                if (self.type==1) {
                    showAlert(@"新增保单成功");
                }
                else if (self.type==2) {
                    showAlert(@"修改保单成功");
                }
                
                // carModel 有值说明从详情页进入,返回时须刷新列表
                if (!ObjectIsEmpty(self.carModel)) {
                    kUserDefaultSetBool(YES, kRefreshStr);
                }
                [self.navigationController popViewControllerAnimated:YES];
            }
           
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
        } withShowHud:YES];
    } onVC:self];
    
   
   
    
    
    
}

-(void)uploadImage:(NSArray *)ary{
    
    if (self.picIndex==1) {
        [self.annexAry1 removeLastObject];
    }
    else {
        [self.annexAry2 removeLastObject];
    }
    
    MBProgressHUD *hud = [MBProgressHUD showGif];
        
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        
    dispatch_async(queue, ^{
    
    [ary enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
        [MSNetworkManager postRevertUrl:POST_UploadFile_URL params:@{@"type":@"1"} formData:@[obj] success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            if (isSuccess) {
                
                NSString *imageUrl = [NSString stringWithFormat:@"%@%@",HTTP_URL,resonseObj[@"url"]];
                if (self.picIndex==1) {
                    [self.annexAry1 addObject:@{
                        @"url":imageUrl
                    }];
                    [self.annexDataAry1 addObject:resonseObj[@"url"]];
                }
                else {
                    [self.annexAry2 addObject:@{
                        @"url":imageUrl
                    }];
                    [self.annexDataAry2 addObject:resonseObj[@"url"]];
                }
                
                if (idx == ary.count-1) {
                    [hud hide];
                    if (self.picIndex==1) {
                        [self.annexAry1 addObject:@{
                            @"url":@"upLoad_B",
                        }];
                    }
                    else {
                        [self.annexAry2 addObject:@{
                            @"url":@"upLoad_B",
                        }];
                    }
                   
                    [self.tableView reloadData];
                }
                dispatch_semaphore_signal(semaphore);
            }
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
            dispatch_semaphore_signal(semaphore);
            [hud hide];
        } withShowHud:NO];
        
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        
        
     }];
    });

}


@end
